package hcs.ops.func;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Web_App {
	
	public void go_to_ops_web_app(WebDriver driver){
		
		String home = "http://eorder-qa.stlukesmedicalcenter.com.ph/epayment/Home.html";
		driver.get(home);
		System.out.println(driver.getTitle());
		
	}
	public void select_location(WebDriver driver, String location){
		
		if(location.equals("GC")){
			driver.findElement(By.xpath("//input[@id='optionsRadios1']")).click();
			
		}else{
			driver.findElement(By.xpath("//input[@id='optionsRadios2']")).click();
		}
		driver.findElement(By.xpath("//input[@value='Submit']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void search_patient(WebDriver driver, String pin, String lastname){
		
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='hcsPin']")));
		driver.findElement(By.xpath("//input[@name='hcsPin']")).sendKeys(pin);
		driver.findElement(By.xpath("//input[@name='lastName']")).sendKeys(lastname);
		
		driver.findElement(By.xpath("//button[@class='btn btn-primary']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		boolean checking = validation(driver);
		if(checking==false){
			search_patient(driver, pin, lastname);
		}else{
			System.out.println("Proceeding to Payment Details");
		}
	}
	public boolean validation(WebDriver driver){
		
		try{
			String message = driver.findElement(By.xpath("//html/body/div[@id='errorPopup']/p")).getText();
			System.out.println(message);
			driver.findElement(By.xpath("//input[@id='errorPopupClose']")).click();
			return false;

		}catch(Exception e){
			return true;
		}
	}
	public String get_current_balance(WebDriver driver, String option){
		double dbl_balance = 0, for_checking;
		String balance = driver.findElement(By.xpath("//span[@id='balanceDue']")).getText();
		balance = balance.replaceAll(",", "");
		balance = balance.replaceAll(" ", "");
		balance = balance.replaceAll("Php", "");
		balance = balance.replaceAll(".00", "");
		System.out.println("Original String for Balance: "+balance);
		String orig_balance = balance;
		double dbl_orig_balance = Double.parseDouble(orig_balance);
		if(option.equals("partial")){
			dbl_balance = Double.parseDouble(balance);
			dbl_balance = dbl_balance / 3;
			balance = Double.toString(dbl_balance);
		}else if(option.equals("decimal")){
			dbl_balance = Double.parseDouble(balance);
			dbl_balance = dbl_balance / 2;
			balance = Double.toString(dbl_balance);
		}else{
			dbl_balance = dbl_orig_balance;
		}
		if(balance.contains(".0")){
			balance = balance.replaceAll(".0", "");
		}
		System.out.println("New String for Balance: "+balance);
		for_checking = dbl_orig_balance - dbl_balance;
		String str_for_checking = Double.toString(for_checking);
		System.out.println("For checking Balance: "+ for_checking);
		balance = str_for_checking +","+balance;
		return balance;
	}
	public String input_email_address(WebDriver driver) throws BiffException, IOException{
		
		String workingDir = System.getProperty("user.dir");
		FileInputStream f2 = new FileInputStream(workingDir+"\\excel\\email.xls"); 
		Workbook wb = Workbook.getWorkbook(f2);
		Sheet sheet = wb.getSheet(0);
		String email = sheet.getCell(0,0).getContents().trim();
		return email;
	}
	public String step2(WebDriver driver, String option) throws BiffException, IOException{
		
		String email = input_email_address(driver);
		String payment = get_current_balance(driver, option);
		String[] arrString = payment.split(",");
		String for_checking = arrString[0];
		String for_payment = arrString[1];
		driver.findElement(By.xpath("//input[@id='amount']")).sendKeys(for_payment);
		if(option.equals("with_guarantor")){
			driver.findElement(By.xpath("//input[@id='notPatient']")).click();
			driver.findElement(By.xpath("//input[@id='holderLastName']")).sendKeys("Guarantor_LastName");
			driver.findElement(By.xpath("//input[@id='holderFirstName']")).sendKeys("Guarantor_FirstName");
			driver.findElement(By.xpath("//input[@id='holderMiddleName']")).sendKeys("Guarantor_MiddleName");
			driver.findElement(By.xpath("//input[@id='holderEmail']")).sendKeys(email);
			driver.findElement(By.xpath("//input[@id='holderAreaCode']")).sendKeys("65");
			driver.findElement(By.xpath("//input[@id='holderNumber']")).sendKeys("999999999");
		}else{
			driver.findElement(By.xpath("//input[@id='emailAddress']")).sendKeys(email);
			driver.findElement(By.xpath("//input[@id='contactNumberAreaCode']")).sendKeys("65");
			driver.findElement(By.xpath("//input[@id='contactNum']")).sendKeys("999999999");
		}
		driver.findElement(By.xpath("//input[@value='Continue']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		return for_checking;
	}
	public void click_agreement(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='btnAccept']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void step_payment(WebDriver driver, String mode_payment){
		if(mode_payment.equals("visa")){
			driver.findElement(By.xpath("//img[@src='/res/svr/default/images/cards/card_sm_visa.gif']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@id='CardNumber']")).sendKeys("4005550000000001");
		}else{
			driver.findElement(By.xpath("//img[@src='/res/svr/default/images/cards/card_sm_masterc.gif']")).click();
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			driver.findElement(By.xpath("//input[@id='CardNumber']")).sendKeys("5123456789012346");
		}
		driver.findElement(By.xpath("//input[@id='CardMonth']")).sendKeys("05");
		driver.findElement(By.xpath("//input[@id='CardYear']")).sendKeys("17");
		driver.findElement(By.xpath("//input[@id='Securecode']")).sendKeys("210");
		driver.findElement(By.xpath("//input[@id='Paybutton']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void view_or(WebDriver driver){
		
		WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(
		    ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-success']")));
		driver.findElement(By.xpath("//button[@class='btn btn-success']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		System.out.println("PLEASE REVIEW THE OR MANUALLY");
		
		Actions actions = new Actions(driver);
        WebElement mouseover1 = driver.findElement(By.xpath("//input[@id='optionsRadios1']"));
        actions.moveToElement(mouseover1);
        actions.click().perform();
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
	}
	public void verifying_payment(WebDriver driver, String for_checking){

		String balance = driver.findElement(By.xpath("//span[@id='balanceDue']")).getText();
		balance = balance.replaceAll(",", "");
		balance = balance.replaceAll(" ", "");
		balance = balance.replaceAll("Php", "");
		
		if(for_checking.contains(".0")||for_checking.contains(".5")){
			for_checking = for_checking+"0";
		}
		
		System.out.println(for_checking);
		System.out.println(balance);
		if(for_checking.equals(balance)){
			System.out.println("CORRECT REMAINDER OF BALANCE AFTER PAYMENT");
		}else{
			System.out.println("INCORRECT REMAINDER OF BALANCE AFTER PAYMENT");
		}
	}
	public void view_webapp_soa(WebDriver driver){
		
		driver.findElement(By.xpath("//a[contains(.,'View Statement of Account')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void newly_admitted(WebDriver driver){

		String balance = driver.findElement(By.xpath("//span[@id='balanceDue']")).getText();
		balance = balance.replaceAll(",", "");
		balance = balance.replaceAll(" ", "");
		balance = balance.replaceAll("Php", "");
	
		System.out.println(balance);
		if(balance.equals("0.00")){
			System.out.println("CORRECT. BALANCE IS ZERO");
		}else{
			System.out.println("INCORRECT. BALANCE SHOULD BE ZERO");
		}
	}
}
